<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>{{ detailOption ? detailOption.name : '' }}编辑教具</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="缩略图:" prop="pic">
            <imgUploadModle @getChildValue="getChildValue" imgNumber="1" page="teachingPic"
                            :picSrc="editForm.pic"></imgUploadModle>
          </el-form-item>
          <el-row>
            <el-col :span="18">
              <el-form-item label="教辅名称:" prop="name">
                <el-input v-model="editForm.name" auto-complete="off" placeholder="填写教辅具名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="入库数量：" prop="num" v-if="!detailOption">
                <el-input v-model="editForm.num" auto-complete="off" placeholder="填写数量"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="教辅分类" prop="cate_id">
                <!--                <div style="display: flex;">-->
                <!--                  <el-select placeholder="教辅分类" v-model="first_id" @change="changeSearchSelectFn(1)" class="mr-10">-->
                <!--                    <el-option :label="item.name" :value="index" v-for="(item, index) in tableList"-->
                <!--                               :key="index"></el-option>-->
                <!--                  </el-select>-->
                <!--                  <el-select placeholder="教辅分类" @change="changeSearchSelectFn(2)" v-if="sonselectList" v-model="son_id">-->
                <!--                    <el-option :label="item.name" :value="item.id" v-for="(item, index) in sonselectList"-->
                <!--                               :key="index"></el-option>-->
                <!--                  </el-select>-->
                <!--                </div>-->
                <el-cascader
                  v-model="editForm.cate_id"
                  size="small"
                  class="mr-5"
                  :options="selectList"
                  :props="props"
                  clearable></el-cascader>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="教辅说明:">
                <el-input v-model="editForm.descs" auto-complete="off" placeholder="教辅说明" type="textarea"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {tools_category} from '@/api/admin/book'

export default {
  props: ['visible', 'detailOption'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      dialogVisible: false,
      editForm: {
        name: '', // 名称
        descs: '',
        num: '', // 库存
        pic: '', // 缩略图
        cate_id: '' // 分类
      }, // 编辑界面数据
      tableList: [],
      sonselectList: null,
      first_id: '',
      son_id: '',
      selectList: [],
      props: {
        label: 'name',
        value: 'id'
      },
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.lazyLoad()
        // this.coteryInitFn()
      } else {
        this.tableList = []
        for (let i in this.editForm) {
          this.editForm[i] = '';
        }
      }
    }
  },
  created() {
  },
  methods: {
    InitFn() {
      this.$fetch(this.api.tools_edit, {id: this.detailOption.id}).then((response) => {
        if (response.code === 0) {
          this.editForm.name = response.vo.name
          this.editForm.descs = response.vo.descs
          this.editForm.num = response.vo.num
          this.editForm.pic = response.vo.pic
          console.log('this.selectList', this.selectList)
          let cate_id = []
          for (let item of this.selectList) {
            if (response.vo.cate_id == item.id) {
              cate_id.push(item.id)
            } else {
              if (item.children) {
                for (let child of item.children) {
                  if (child.id == response.vo.cate_id) {
                    cate_id = [item.id, child.id]
                  }
                }
              }
            }
          }
          this.editForm.cate_id = cate_id
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    lazyLoad() {
      this.$fetch(this.api.tools_category_index, {p: 1}).then((response) => {
        if (response.code === 0) {
          this.selectList = this.$handleTree(response.list, 'id', 'tid')
          if (this.detailOption.id) {
            this.InitFn()
          }
        } else {
          console.log('请求失败')
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      // if(!dataOption.pic){
      //   this.$message.error('上传教具缩略图')
      //   return
      // }
      if (!dataOption.name) {
        this.$message.error('填写教具名称')
        return
      }
      if (!dataOption.num && !detailOption) {
        this.$message.error('填写入库数量')
        return
      }
      if (!dataOption.cate_id || !dataOption.cate_id.length) {
        this.$message.error('选择教具分类')
        return
      }
      let url = this.api.tools_add
      if (this.detailOption) {
        url = this.api.tools_edit
        dataOption.id = this.detailOption.id
      }
      dataOption.cate_id = dataOption.cate_id[dataOption.cate_id.length-1]
      let data = await this.$post(url, dataOption);
      if (!data.code) {
        this.$message.success('编辑成功！')
        this.$emit('InitFn')
        this.closePOpup()
      } else {
        this.$message.error(data.msg)
      }
    },
    getChildValue(option) {
      this.editForm.pic = option
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 550px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


